package cn.com.xf.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.xf.common.StringUtil;
import cn.com.xf.dao.TblApiAppDAO;
import cn.com.xf.dao.TblApiBarDao;
import cn.com.xf.dao.TsSiteinfoDAO;
import cn.com.xf.domain.TblApiApp;
import cn.com.xf.domain.TblApiBar;
import cn.com.xf.domain.TsSiteinfo;
import cn.com.xf.service.TblApiBarService;
import cn.com.xf.web.form.BarForm;

@Service
public class TblApiBarServiceImpl implements TblApiBarService {

	@Autowired
	private TblApiBarDao tbbarDAO;
	@Autowired
	private TblApiAppDAO apiAppDAO;
	@Autowired
	private TsSiteinfoDAO tsSiteinfoDAO;

	@Override
	public int insertTbBar(BarForm form) {
		TblApiBar tbbar = new TblApiBar();
		tbbar.setDeviceid(form.getDeviceid());
		tbbar.setBrand(form.getBrand());
		tbbar.setCompanyName(form.getCompanyName());
		tbbar.setSiteid(form.getSiteid());
		tbbar.setAddUser(form.getAddUser());
		tbbar.setAddTime(new Date());
		tbbar.setRemark(form.getRemark());
		tbbar.setAddTimeStr(DateFormatUtils.format(tbbar.getAddTime(), "yyyy-MM-dd HH:mm:ss"));
		tbbarDAO.insert(tbbar);
		return 0;
	}

	@Override
	public TblApiBar selectByDeviceID(String deviceId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TblApiBar> select(int begin, int pageSize, String orderByField, String orderByMode, String siteid,
			String brand, String deviceid) {
		List<TblApiBar> list = tbbarDAO.selectBar(begin, pageSize, orderByField, orderByMode, siteid, brand, deviceid);
		return list;
	}

	@Override
	public int selectCount(String siteid, String brand, String deviceid) {

		return tbbarDAO.selectCount(siteid, brand, deviceid);
	}

	@Override
	public int delete(String id) {
		return tbbarDAO.deleteByPrimaryKey(new BigDecimal(id));
	}

	@Override
	public List<TblApiApp> selectAllBrand() {
		return apiAppDAO.selectAll();
	}

	@Override
	public List<TsSiteinfo> selectSites(String term) {
		if (StringUtil.isNumeric(term)) {
			return tsSiteinfoDAO.selectBySiteid(term);
		} else if (StringUtil.isChinese(term)) {
			return tsSiteinfoDAO.selectBySitename(term);
		} else {
			return tsSiteinfoDAO.selectByCode(term);
		}
	}

	@Override
	public int insertApiApp(TblApiApp app) {
		apiAppDAO.insert(app);
		return 0;
	}

	@Override
	public int selectAppCount(String companyName, String appType, String appStatus) {
		return apiAppDAO.selectCount(companyName, appType, appStatus);
	}

	@Override
	public List<TblApiApp> selectApp(int begin, int pageSize, String orderByField, String orderByMode,
			String companyName, String appType, String appStatus) {
		return apiAppDAO.selectApp(begin, pageSize, orderByField, orderByMode, companyName, appType, appStatus);
	}

	@Override
	public int deleteAppById(String id) {
		return apiAppDAO.deleteByPrimaryKey(Integer.valueOf(id));
	}

	@Override
	public TblApiApp selectAppById(String id) {
		return apiAppDAO.selectByPrimaryKey(Integer.valueOf(id));
	}

	@Override
	public int updateApiApp(TblApiApp app) {
		return apiAppDAO.updateByPrimaryKeySelective(app);
	}

	@Override
	public List<TsSiteinfo> selectSites() {
		return tsSiteinfoDAO.selectSites();
	}

	@Override
	public int insertBatchBar(List<TblApiBar> list) {
		return tbbarDAO.batchInsert(list);
	}
}
